package org.vectrics.recruiter.user;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.vectrics.DataException;
import org.vectrics.common.db.Criteria;
import org.vectrics.hibernate.Filter;
import org.vectrics.hibernate.Selector;
import org.vectrics.hibernate.ThreadProperties;

public class InterviewSearcher {
	private transient final Logger log = Logger.getLogger(this.getClass());
	public static String BEAN_ID = "interview.searcher";
	
	public int findInterviewCount(InterviewCriteria criteria) {
		Session session = ThreadProperties.getSession();
		int count = 0;

		Filter filter = getFilterForCriteria(criteria, false);
		filter.addSelectField("count(distinct interview.id)");
		Query query = filter.createQuery(session);
		Iterator results = query.iterate();

		if (results == null) {
			throw new DataException("Method does not define logic for criteria");
		}
		else {
			while ( results.hasNext() ) {
				Object row = (Object)results.next();
				Long countInteger = (Long)row;
				count = countInteger.intValue();
			}
		}
		return(count);
	}
	public List findInterviews(InterviewCriteria criteria, int startIndex, int endIndex) {
		List elements = null;
		Session session = ThreadProperties.getSession();

		Filter filter = getFilterForCriteria(criteria, true);
		filter.addSelectField("interview");

		Query query = null;
		query = filter.createQuery(session);

		if (log.isDebugEnabled()) {
			log.debug("Statement = " + filter.getStatement());
		}

		if ((endIndex >= 0) && (startIndex >= 0)) {
			query.setMaxResults(1 + endIndex - startIndex);
			query.setFirstResult(startIndex);
		}
		elements = query.list();
		return(elements);
	}

	protected Filter getFilterForCriteria(InterviewCriteria criteria, boolean ordered) {
		log.debug("getFilterForCriteria()");
		Filter filter = new Filter();
		filter.setConjunction(Selector.CONJUNTION_AND);
		filter.addFrom("org.vectrics.recruiter.user.Interview", "interview");
		
		if (ordered) {
			filter.addOrderBy("interview.dateStart desc");
		}


		if (criteria.getVoidedType() == Criteria.VOID_YES)
			filter.addCondition("interview.voided='T'");
		else if (criteria.getVoidedType() == Criteria.VOID_NO)
			filter.addCondition("interview.voided='F'");

		if (criteria.getInterviewer() != null) {
			filter.addParameterCondition("interview.person.id", "=", criteria.getInterviewer().getId());
		}
		if (criteria.getJob() != null) {
			filter.addParameterCondition("interview.job.id", "=", criteria.getJob().getId());
		}
		if ((criteria.getStatusCodes() != null) && (criteria.getStatusCodes().size() > 0)) {
			Selector childSelect = filter.getSelector().addChildSelector(Selector.CONJUNTION_AND);
			Iterator iter = criteria.getStatusCodes().iterator();
			while (iter.hasNext()) {
				String code = (String)iter.next();
				childSelect.addParameterCondition("interview.statusType.code", "=", code);
			}
		}
		return(filter);
	}
	
	public List<InterviewInstance> getInterviewInstances(){
		Session session = ThreadProperties.getSession();
		
		org.hibernate.Criteria criteria = session.createCriteria(InterviewInstance.class);
		return criteria.list();
	}
	
	public InterviewInstance getInterviewInstance(String code){
		Session session = ThreadProperties.getSession();
		
		org.hibernate.Criteria criteria = session.createCriteria(InterviewInstance.class);
		criteria.add(Restrictions.eq("code", code));
		return (InterviewInstance) criteria.uniqueResult();
	}
}
